import type { App } from 'vue'
import Layout from '@/layout/index.vue'
import { createRouterGuards } from '@/router/routerGuards'
import { createRouter, createWebHashHistory, RouteRecordRaw } from 'vue-router'

const modules = import.meta.globEager('./modules/**/*.ts')
const routeModuleList: RouteRecordRaw[] = []
Object.keys(modules).forEach((key) => {
  const mod = modules[key].default || {}
  const modList = Array.isArray(mod) ? [...mod] : [mod]
  routeModuleList.push(...modList)
})

const routes: Array<RouteRecordRaw> = [
  {
    path: '/',
    name: 'Layout',
    component: Layout,
    redirect: '/report',
    children: [...routeModuleList]
  }
]

const router = createRouter({
  history: createWebHashHistory(),
  routes
})

// config router
export function setupRouter(app: App<Element>) {
  app.use(router)
  createRouterGuards(router)
}

export default router
